<template>
	<view>
		<view class="plr-36 mt-20">
			<u-search v-model="addr" placeholder="请输入地址" @change="bindInput"></u-search>
		</view>
		<block v-if="list">
			<view class="plr-46 ptb-20 bb" @click="search(item)" v-for="(item,i) in list" :key="i" >
				<view class="">
					{{item.name}}
				</view>
				<view class="size-26 gray-2" v-if="item&&item.address">
					{{item.id?item.district:''}}{{item.address.length==0?'':item.address}}
				</view>
			</view>
		</block>
	</view>
</template>

<script>
	import {mapState} from 'vuex'
	import indexConfig from '@/config/index.config';
	import amapFile from '@/utils/amap-wx.130.js'
	export default {
		data() {
			return {
				addr: '',
				list: [],
				items:[],//历史搜索记录
				value:0,//0=没有历史搜索，1=有历史搜索
				type:'' ,//1 起点  2终点 3货运终点
				// address: uni.getStorageSync('address') || [], //默认搜索的地址
			}
		},
		computed:{
			...mapState(['address','addressHistory'])
		},
		onLoad(option) {
			this.type = option.type
			this.location = option.location
			console.log( "uni.getStorageSync('address')",uni.getStorageSync('address') )
			console.log( " this.address", this.address)
			console.log( "uni.getStorageSync('addressHistory')",uni.getStorageSync('addressHistory') )
			console.log( " this.addressHistory", this.addressHistory)
		    this.list = this.addressHistory
		},
		methods: {
			bindInput: function(e) {
				var that = this;
				var keywords = e;
				var key = '0b251316ca378180432d141a836831f6';
				// var key = indexConfig.amapApiKeyWeb;
				var myAmapFun = new amapFile.AMapWX({
					key: key
				});
				myAmapFun.getInputtips({
					keywords: keywords,
					location: that.location,
					success: function(data) {
						console.log(data);
						if (data && data.tips) {
							that.list = data.tips
							// uni.setStorageSync('searchlist',that.list )
						}
					}
				})
			},
			
			search(res) {
				console.log(11111111,res);
				
			
				// {
				// 	"id": "B01730K2X2",
				// 	"name": "郑州站",
				// 	"district": "河南省郑州市二七区",
				// 	"adcode": "410103",
				// 	"location": "113.658097,34.745795",
				// 	"address": "二马路82号",
				// 	"typecode": "150200",
				// 	"city": []
				// }
				if(res.id){
					let city;
					
					
					// let addinfo = this.mapfun.matchAddresInfo(res.address)
					let addinfo_temp = this.mapfun.matchAddresInfo(res.district)
					// console.log('addinfo',addinfo)
					// province = addinfo[0]
					// city = addinfo[1]
					// area = addinfo[2]
					console.log("addinfo_temp",addinfo_temp);
					
					let addinfo  = []
					//直辖等行政
					if (res.district.indexOf("省") == -1) {
						addinfo[0] = addinfo_temp[0]
						addinfo[1]= addinfo_temp[0]
						addinfo[2]= addinfo_temp[1]
					}else{
						addinfo[0]= addinfo_temp[0]
						addinfo[1]= addinfo_temp[1]
						addinfo[2]= addinfo_temp[2]
					}
					
						console.log("addinfo",addinfo);
						
					// // 第一部:判断有没有市
					// if(res.district.indexOf('市') != -1){
					// 	// 第二部:获取第一个市的索引
					// 	let num = res.district.indexOf('市')
					// 	// 第三部:判断有没有第二个市
					// 	if (res.district.indexOf('市', num + 1) == -1) {
					// 		if (!res.district.includes('县')) {
					// 			if (res.district.split('市')[0].includes('省')) {
					// 				//搜索到区
					// 				city = res.district.split('市')[0].split('省')[1] + '市'
					// 			} else {
					// 				city = res.district.split('市')[0] + '市'
					// 			}
					// 		} else {
					// 			//搜索到县
					// 			city = res.district.split('市')[1].split('县')[0] + '县'
					// 		}
					// 	} else {
					// 		// 搜索到（县级市）
					// 		city = res.district.split('市')[1] + '市'
					// 	}
					// }else{
					// 	// 搜索到（直辖县）
					// 	city = res.district.split('省')[1]
					// }
					
					
					
					
					
				console.log("res.location",res.location)
					
						
			
					let address = {
						address: res.district + res.address,
						latitude:(res.location!=undefined && res.location.length>0) ? res.location.split(',')[1] : '',
						longitude:(res.location!=undefined && res.location.length>0) ? res.location.split(',')[0] : '',
						name: res.name,
						// city:city,
						province: addinfo[0],
						city: addinfo[1],
						area: addinfo[2],
						district: res.district,
						address1: res.address
					}
					console.log(address)
				
					if(this.type==1){
						this.$store.commit('getStart', address)
					}else if(this.type==2){
						this.$store.commit('getEnd', address)
					}else if(this.type==3){
						this.$store.commit('getHyEnd', address)
					}
				}else{
					// 	console.log('res',res)
					// let addinfo = this.mapfun.matchAddresInfo(res.district)
					// console.log('addinfo',addinfo)
					// city = addinfo[0]
					// city = addinfo[1]
					// city = addinfo[2]
					if(this.type==1){
						this.$store.commit('getStart', res)
					}else if(this.type==2){
						this.$store.commit('getEnd', res)
					}else if(this.type==3){
						this.$store.commit('getHyEnd', res)
					}
				}
				uni.navigateBack({
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
uni-page-body {
		color: $gray !important;
		font-size: 30rpx !important;
	}

	// 自定义头部，状态栏盒子样式
	/* #ifdef APP-PLUS */
	.status_bar {
		height: var(--status-bar-height);
		width: 100%;
	}
	/* #endif */
	

	// tab切换选中加下边框
	.active-bb {
		&::after {
			content: '';
			position: absolute;
			width: 90%;
			height: 5rpx;
			background-color: $blue;
			left: 5%;
			bottom: -25rpx;
		}
	}

	// filed
	.u-field {
		padding: 26rpx 34rpx !important;

		.u-label-text {
			font-weight: 700 !important;
		}
	}

	// 提交按钮
	.u-btn--primary {
		background: linear-gradient(to right,#00b6b4,#00d496) !important;
		border-radius: 0rpx !important;
		margin: 0rpx !important;
		width: 100% !important;
	}

	.u-size-default {
		height: 90rpx !important;
		font-weight: 700 !important;
	}

	// cell组件右边箭头对其文字
	.u-cell_title {
		// font-weight: 700 !important;
		color: #333 !important;
	}

	.u-cell__right-icon-wrap {
		height: 55rpx !important;
	}



.gray-2{
	color:$gray-2;
}

.bg-gray-2{
	background-color:$gray-2;
}


.bb {
    border-bottom: 1px solid #f0f0f0;
}

.pd-46, .plr-46, .pr-46 {
    padding-right: 23px;
}

.pd-46, .plr-46, .pl-46 {
    padding-left: 23px;
}

.pd-20, .ptb-20, .pb-20 {
    padding-bottom: 10px;
}

.pd-20, .ptb-20, .pt-20 {
    padding-top: 10px;
}


.size-26 {
    font-size: 13px;
}

.gray-2 {
    color: #999;
}
</style>
